//
//  XLTool.m
//  iBeaconDemo
//
//  Created by 孔凡伍 on 2017/4/20.
//  Copyright © 2017年 kongfanwu. All rights reserved.
//

#import "XLTool.h"

@implementation XLTool
/**
 计算距离
 
 @param rssi 当前rssi值
 @return 距离，单位米
 */
+ (float)calcDistByRSSI:(NSInteger)rssi
{
    /*
     实际测试 1米rssi值65
     2.0 环境衰减因子
     */
    NSInteger iRssi = labs(rssi);
    float power = (iRssi-70)/(10*2.0);
    return pow(10, power);
}

- (double)calculateAccuracy:(int)txPower rssi:(double)rssi
{
    if (rssi == 0) {
        return -1.0;
    }
    
    double ratio = rssi*1.0/txPower;
    if (ratio < 1.0) {
        return pow(ratio, 10); // a的b次方
    } else {
        double accuracy =  (0.89976) * pow(ratio, 7.7095) + 0.111;
        return accuracy;
    }
}

- (double)calculateAccuracy2:(int)txPower rssi:(double)rssi
{
    if (rssi == 0) {
        return -1.0;
    }
    
    double ratio = rssi*1.0/txPower;
    if (ratio < 1.0) {
        return pow(ratio, 10); // a的b次方
    } else {
        double accuracy = (0.42093) * pow(ratio, 6.9476) + 0.54992;
        return accuracy;
    }
}
@end
